package com.ycy.util;

import java.util.Arrays;
import java.util.List;

import com.google.api.services.plus.model.ActivityObjectAttachments;
import com.google.api.services.plus.model.Activity.PlusObject.Attachments;
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
import com.google.api.client.auth.oauth2.BearerToken;
import com.google.api.client.auth.oauth2.ClientParametersAuthentication;
import com.google.api.client.auth.oauth2.MemoryCredentialStore;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.ycy.ss.Constant;
import com.ycy.ss.sync.AllType;


public class GoogleUtil{
	public static String typeToAllType(String type){
		if ("text".equals(type)) {
			return AllType.TEXT;
		} else if ("link".equals(type)) {
			return AllType.LINK;
		} else if ("video".equals(type)) {
			return AllType.VIDEO;
		} else if ("audio".equals(type)) {
			return AllType.AUDIO;
		} else if ("photo".equals(type)) {
			return AllType.PHOTO;
		}
		return null;
	}

	public static String attachmentToAllType(List<Attachments> attachments){
		if (attachments == null || attachments.size() == 0) {
			return AllType.TEXT;
		}
		Attachments firstAttachment = attachments.get(0);
		if (attachments.size() >= 2 && !"photo".equals(firstAttachment.getObjectType())
				&& "photo".equals(attachments.get(1).getObjectType())) {
			return AllType.PHOTO;
		} else if ("photo".equals(firstAttachment.getObjectType())) {
			return AllType.PHOTO;
		} else if ("article".equals(firstAttachment.getObjectType())) {
			return AllType.LINK;
		}
		return null;
	}

	private static final String GOOGLE_OAUTH2_AUTH_URI = "https://accounts.google.com/o/oauth2/auth";

	private static final String GOOGLE_OAUTH2_TOKEN_URI = "https://accounts.google.com/o/oauth2/token";

	public static final JsonFactory JSON_FACTORY = new GsonFactory();

	public static final HttpTransport TRANSPORT = new NetHttpTransport();

	private static AuthorizationCodeFlow authorizationCodeFlow = null;

	/**
	 * A simple HTML tag stripper to prepare HTML for rendering. This is a quick and dirty solution intended to improve
	 * presentation. Please do not depend on it to prevent XSS attacks.
	 * 
	 * @return The same string with all xml/html tags stripped.
	 */
	public static String stripTags(String input){
		return input.replaceAll("\\<[^>]*>", "");
	}

	public static AuthorizationCodeFlow getFlow(){
		if (null == authorizationCodeFlow) {
			authorizationCodeFlow = new AuthorizationCodeFlow.Builder(BearerToken.authorizationHeaderAccessMethod(),
					TRANSPORT, JSON_FACTORY, new GenericUrl(GOOGLE_OAUTH2_TOKEN_URI),
					new ClientParametersAuthentication(Constant.getAccountInfo("google.oauth_client_id"), Constant
							.getAccountInfo("google.oauth_client_secret")), Constant
							.getAccountInfo("google.oauth_client_id"), GOOGLE_OAUTH2_AUTH_URI).setCredentialStore(
					new MemoryCredentialStore()).setScopes(
					Arrays.asList(Constant.getAccountInfo("google.oauth_scopes"))).build();
		}

		return authorizationCodeFlow;
	}
}
